﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_PerRoleDAL : IDcms_PerRoleDAL
    {
        public Dcms_PerRoleModel AddModel(Dcms_PerRoleModel model)
        {
            string sQLString = "insert into Dcms_PerRole(PerRole_CId,PerRole_RId,PerRole_IsRead,PerRole_IsAdd,PerRole_IsEdit,PerRole_IsDelete) values(@PerRole_CId,@PerRole_RId,@PerRole_IsRead,@PerRole_IsAdd,@PerRole_IsEdit,@PerRole_IsDelete) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@PerRole_CId", SqlDbType.Int), new SqlParameter("@PerRole_RId", SqlDbType.Int), new SqlParameter("@PerRole_IsRead", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsAdd", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsEdit", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsDelete", SqlDbType.TinyInt) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.PerRole_CId;
            cmdParms[2].Value = model.PerRole_RId;
            cmdParms[3].Value = model.PerRole_IsRead;
            cmdParms[4].Value = model.PerRole_IsAdd;
            cmdParms[5].Value = model.PerRole_IsEdit;
            cmdParms[6].Value = model.PerRole_IsDelete;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.PerRole_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_PerRoleModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_PerRoleModel> list = new List<Dcms_PerRoleModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_PerRoleModel item = new Dcms_PerRoleModel();
                if (ds.Tables[0].Rows[i]["PerRole_Id"].ToString() != "") item.PerRole_Id = int.Parse(ds.Tables[0].Rows[i]["PerRole_Id"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_CId"].ToString() != "") item.PerRole_CId = int.Parse(ds.Tables[0].Rows[i]["PerRole_CId"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_RId"].ToString() != "") item.PerRole_RId = int.Parse(ds.Tables[0].Rows[i]["PerRole_RId"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_IsRead"].ToString() != "") item.PerRole_IsRead = int.Parse(ds.Tables[0].Rows[i]["PerRole_IsRead"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_IsAdd"].ToString() != "") item.PerRole_IsAdd = int.Parse(ds.Tables[0].Rows[i]["PerRole_IsAdd"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_IsEdit"].ToString() != "") item.PerRole_IsEdit = int.Parse(ds.Tables[0].Rows[i]["PerRole_IsEdit"].ToString());
                if (ds.Tables[0].Rows[i]["PerRole_IsDelete"].ToString() != "") item.PerRole_IsDelete = int.Parse(ds.Tables[0].Rows[i]["PerRole_IsDelete"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_PerRole where PerRole_Id=@PerRole_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PerRole_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_PerRoleModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_PerRole where PerRole_Id=@PerRole_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PerRole_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_PerRoleModel model = new Dcms_PerRoleModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_PerRoleModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public Dcms_PerRoleModel GetModelByPIdAndRId(int CateID, int RoleId)
        {
            string sQLString = "select top 1 * from Dcms_PerRole where PerRole_CId=@PerRole_CId And PerRole_RId=@PerRole_RId";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PerRole_CId", SqlDbType.Int), new SqlParameter("@PerRole_RId", SqlDbType.Int) };
            cmdParms[0].Value = CateID;
            cmdParms[1].Value = RoleId;
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_PerRoleModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_PerRole";
            return SqlHelper.CreateDataSet(sql);
        }

        public IList<Dcms_PerRoleModel> GetPIdByRId(int RId, string Flag)
        {
            string sQLString = "select * from Dcms_PerRole where PerRole_RId=@PerRole_RId And " + Flag + "=1";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PerRole_RId", SqlDbType.Int) };
            cmdParms[0].Value = RId;
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            return this.DataSetToList(ds);
        }

        public Dcms_PerRoleModel Update(Dcms_PerRoleModel model)
        {
            string sQLString = "update Dcms_PerRole set PerRole_IsRead=@PerRole_IsRead,PerRole_IsAdd=@PerRole_IsAdd,PerRole_IsEdit=@PerRole_IsEdit,PerRole_IsDelete=@PerRole_IsDelete where PerRole_CId=@PerRole_CId and PerRole_RId=@PerRole_RId";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PerRole_CId", SqlDbType.Int), new SqlParameter("@PerRole_RId", SqlDbType.Int), new SqlParameter("@PerRole_IsRead", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsAdd", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsEdit", SqlDbType.TinyInt), new SqlParameter("@PerRole_IsDelete", SqlDbType.TinyInt) };
            cmdParms[0].Value = model.PerRole_CId;
            cmdParms[1].Value = model.PerRole_RId;
            cmdParms[2].Value = model.PerRole_IsRead;
            cmdParms[3].Value = model.PerRole_IsAdd;
            cmdParms[4].Value = model.PerRole_IsEdit;
            cmdParms[5].Value = model.PerRole_IsDelete;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

